package com.wcar.store.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.StoreLabels;
import com.wcar.common.entity.User;
import com.wcar.common.entity.UserRole;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.store.dao.LablesDao;
import com.wcar.store.dao.UserRoleDao;
import com.wcar.store.service.LablesService;
import com.wcar.store.vo.StoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @BelongsProject: four-wcar-idea
 * @BelongsPackage: com.wcar.store.service.impl
 * @Author: 李睢康
 * @CreateTime: 2024-05-28  18:46
 */
@Service
public class LablesServiceImpl implements LablesService {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Resource
    private LablesDao lablesDao;
    @Resource
    private UserRoleDao userRoleDao;
    @Override
    public Result<IPage> getAll(StoreVo storeVo) {
        IPage<StoreLabels> iPage=new Page<>(storeVo.getCurrentPage(),storeVo.getPageSize());
        QueryWrapper<StoreLabels> wrapper=new QueryWrapper<>();
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<UserRole> wrapper1=new QueryWrapper<>();
        wrapper1.eq("user_id",user.getUserId());
        List<UserRole> userRoles = userRoleDao.selectList(wrapper1);
        ArrayList<Integer> roleIds=new ArrayList<>();
        for (UserRole u: userRoles) {
            roleIds.add(u.getRoleId());
        }
        //判断是不是总店长
        if(!roleIds.contains(1)){
            wrapper.eq("tsu.user_id",user.getUserId());
        }
        if(storeVo.getStoreName()!=null&&storeVo.getStoreName()!=""){
            wrapper.like("ts.store_name",storeVo.getStoreName());
        }
        IPage<StoreLabels> storeLabelsIPage = lablesDao.selectPageByname(iPage, wrapper);
        return new Result<>(200,"获取所有标签成功",storeLabelsIPage);
    }

    @Override
    public Result<Integer> insert(StoreLabels storeLables) {
        int insert = lablesDao.insert(storeLables);
        return new Result<>(200,"添加标签成功",insert);
    }

    @Override
    public Result<Integer> update(StoreLabels storeLabels) {
        int i = lablesDao.updateById(storeLabels);
        return new Result<>(200,"修改标签成功",i);
    }

    @Override
    public Result<Integer> delete(Integer labelId) {
        int i = lablesDao.deleteById(labelId);
        return new Result<>(200,"删除标签成功",i);
    }
}
